package com.wangan.wanganpush.services;

import com.wangan.wanganpush.dao.OrderInfoDao;
import com.wangan.wanganpush.model.UserOrderInfo;
import com.wangan.wanganpush.utils.FileNameParse;
import com.wangan.wanganpush.xml.UserOrderInfoXml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class OrderInfoImpl extends BaseImpl{

    @Autowired
    OrderInfoDao orderInfoDao;

    @Autowired
    GetXmlSendZipUtil getXmlSendZipUtil;

    @Autowired
    FileNameParse fileNameParse;

    public Boolean userOrderInfoPush() {
        try {
            List<UserOrderInfo> li = orderInfoDao.findAllByFlag(0);
            if (li != null && li.size() > 0) {
                //拿取消息流水号
                String messageCode = fileNameParse.getMessageCode();

                String fileName =fileNameParse.parseFileName("VEHICLEBOOK","3",messageCode);
                //把数据转xml字符串
                String str = UserOrderInfoXml.parseXml(li,messageCode);

                //送往xml转zip包的流程并推送网安
                getXmlSendZipUtil.sendZip("VEHICLEBOOK",str,messageCode,fileName,null);
            }
            System.out.println(li.size());


            super.updateFlag(li);

            //更改数据库flag字段为1已经发送
            if (li != null && li.size() > 0) {
                //更新数据
                orderInfoDao.save(li);
                //提交事务
                orderInfoDao.flush();
            }

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
